package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Admin;
import com.neusoft.entity.AdminQuery;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.AdminService;
import com.neusoft.service.StudentService;
import feign.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;


/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-05-26 09:13:48
 */

@Controller
@RequestMapping("admin")
public class AdminController {

      private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 服务对象
     */
    @Resource//@Autowire
    private AdminService adminService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("toLogin")
    public String toLogin() {
        logger.trace("trace是最低级的");
        logger.debug("第二");
        logger.info("第三");
        logger.warn("第四");
        logger.error("第五");
        return "login";
    }

    @RequestMapping("toWelcome")
   // @RequestMapping("/")
    public String toWelcome() {
        return "welcome";
    }

    @RequestMapping("toList")
    public String toList(){
        return "user/admin_list";
    }


    @RequestMapping("toAdd")
    //@GetMapping("/")
    public String toAdd(Model model){
        model.addAttribute("bean",new Admin());
        return "user/admin_add";
    }

    //@RequestMapping("toEdit")
    @GetMapping("/{id}")
    public String toEdit(@PathVariable Integer id,Model model){
        System.out.println("id:::"+ id);
        Admin bean = adminService.queryById(id);
        model.addAttribute("bean",bean);
        return "user/admin_add";
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     *
     */
    @GetMapping("getById/{id}")
    @ResponseBody
    public Admin getById(@PathVariable Integer id) {
        return this.adminService.queryById(id);
    }




    @PostMapping("login")
    public String login(@RequestParam("acount")String account, @RequestParam("password")String password, Model model, HttpServletRequest request) {
        //将用户信息存入到session
        System.out.println("登录的账户和密码：account::" + account + "password::" + password);
        HttpSession session = request.getSession();
        session.setAttribute("account", account);
        System.out.println("account：：：" + session.getAttribute("account"));
        Admin admin = adminService.login(account, password);
        if (admin == null) {
            //登录不成功
            model.addAttribute("msg", "账号或密码错误");
            System.out.println("登录不成功画面。。。。。。。。。");
            return "login";
        }
           //使用Cookie记住账号和密码
        return "index";
    }

    //分页查询
    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(@RequestParam(value = "page",required=false)Integer page, @RequestParam(value = "limit",required=false)Integer limit, @RequestBody AdminQuery bean){
        /*System.out.println("page:::" + page);
        System.out.println("limit:::" + limit);
        System.out.println("bean:::" + bean);*/
       CommonResult<Admin> result = new CommonResult<>();
        IPage<Admin> iPage = adminService.queryAllByLimit(page,limit,bean);
        result.setCode(0);
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }
/*
    @PostMapping("save")
    @ResponseBody
   public Object save(Admin bean){
         boolean result = false;
        //判断账号是否存在

        //判断是添加还是编辑
        if(bean.getId() != null) {
            //编辑
            result = adminService.update(bean);
        }else{
            //添加
        bean.setRegDate(new Date());
        result = adminService.insert(bean).getId() !=null;
        }
        return result;
   }*/

   /* @PostMapping("insert")
    @ResponseBody
    public Admin insert(Admin bean) {
        return adminService.insert(bean);
    }*/

   /* @DeleteMapping("/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids){
        if(ids == null || ids.length == 0){
            return false;
        }
        adminService.deleteById(Arrays.asList(ids));
        return true;
    }*/
}
